1. Field of Invention
The present invention pertains to the computing sciences generally, and, more specifically to an apparatus and method of an instruction that determines whether a value is within a range.
2. Background
FIG. 1 shows a high level diagram of a processing core 100 implemented with logic circuitry on a semiconductor chip. The processing core includes a pipeline 101. The pipeline consists of multiple stages each designed to perform a specific step in the multi-step process needed to fully execute a program code instruction. These typically include at least: 1) instruction fetch and decode; 2) data fetch; 3) execution; 4) write-back. The execution stage performs a specific operation identified by an instruction that was fetched and decoded in prior stage(s) (e.g., in step 1) above) upon data identified by the same instruction and fetched in another prior stage (e.g., step 2) above). The data that is operated upon is typically fetched from (general purpose) register storage space 102. New data that is created at the completion of the operation is also typically “written back” to register storage space (e.g., at stage 4) above).
The logic circuitry associated with the execution stage is typically composed of multiple “execution units” or “functional units” 103_1 to 103_N that are each designed to perform its own unique subset of operations (e.g., a first functional unit performs integer math operations, a second functional unit performs floating point instructions, a third functional unit performs load/store operations from/to cache/memory, etc.). The collection of all operations performed by all the functional units corresponds to the “instruction set” supported by the processing core 100.
Two types of processor architectures are widely recognized in the field of computer science: “scalar” and “vector”. A scalar processor is designed to execute instructions that perform operations on a single set of data, whereas, a vector processor is designed to execute instructions that perform operations on multiple sets of data. FIGS. 2A and 2B present a comparative example that demonstrates the basic difference between a scalar processor and a vector processor.
FIG. 2A shows an example of a scalar AND instruction in which a single operand set, A and B, are ANDed together to produce a singular (or “scalar”) result C (i.e., AB=C). By contrast, FIG. 2B shows an example of a vector AND instruction in which two operand sets, A/B and D/E, are respectively ANDed together in parallel to simultaneously produce a vector result C, F (i.e., A.AND.B=C and D.AND.E=F). As a matter of terminology, a “vector” is a data element having multiple “elements”. For example, a vector V=Q, R, S, T, U has five different elements: Q, R, S, T and U. The “size” of the exemplary vector V is five (because it has five elements).
FIG. 1 also shows the presence of vector register space 104 that is different that general purpose register space 102. Specifically, general purpose register space 102 is nominally used to store scalar values. As such, when, the any of execution units perform scalar operations they nominally use operands called from (and write results back to) general purpose register storage space 102. By contrast, when any of the execution units perform vector operations they nominally use operands called from (and write results back to) vector register space 107. Different regions of memory may likewise be allocated for the storage of scalar values and vector values.
Note also the presence of masking logic 104_1 to 104_N and 105_1 to 105_N at the respective inputs to and outputs from the functional units 103_1 to 103_N. In various implementations, only one of these layers is actually implemented—although that is not a strict requirement. For any instruction that employs masking, input masking logic 104_1 to 104_N and/or output masking logic 105_1 to 105_N may be used to control which elements are effectively operated on for the vector instruction. Here, a mask vector is read from a mask register space 106 (e.g., along with input data vectors read from vector register storage space 107) and is presented to at least one of the masking logic 104, 105 layers.
Over the course of executing vector program code each vector instruction need not require a full data word. For example, the input vectors for some instructions may only be 8 elements, the input vectors for other instructions may be 16 elements, the input vectors for other instructions may be 32 elements, etc. Masking layers 104/105 are therefore used to identify a set of elements of a full vector data word that apply for a particular instruction so as to effect different vector sizes across instructions. Typically, for each vector instruction, a specific mask pattern kept in mask register space 106 is called out by the instruction, fetched from mask register space and provided to either or both of the mask layers 104/105 to “enable” the correct set of elements for the particular vector operation.
FIGS. 3A and 3B pertain to known approaches for keeping generated candidate values within a range S. It is common that software needs to generate a value in a limited range, such as 0 to 255, or, 1 to 1000, or, −3.1415 to 3.1415. It is also common that candidate values (e.g., as generated by an algorithm) fall outside the range. According to one approach, observed in FIG. 3A, a new candidate value is generated 301 and a conditional test tests to see if the new candidate value is out of range 302. Upon detection of an out of range value, subsequent program flow “pulls” the out of range value “back into range”. More simply stated, a new “replacement” value is created for the out of range value that is within the range 303.
According to one approach, the new replacement value is calculated by adding the “excess” of the out of range value to the low end of the range. That is, a new candidate value C′ is generated from the original out of range candidate C according to C′=(((C−A) % S)+A) where S is the size of the range, A is the beginning of the range and % is a modulus function. The modulus function % divides how far C extends from the beginning of the range (C−A) and presents the remainder of the division as a result. For example, if the range is from 0 to 256 and C=300, then (C−A) % S=(300−0) % 256=44. As such, the new replacement value would be computed as 44+A=44+0=44.
The modulus function, however, is expensive to implement and is usually a slower operation.
As such, alternate approaches have been taken. One particular approach, observed in FIG. 3B, simplifies the calculation of a new replacement candidate so as to avoid use of the modulus function in cases where a next generated candidate is some small increment of a previous value. For example, consider an algorithm that generates a new candidate value by incrementing a previous value by 4 (e.g., C=254+4=258). In cases where the increment amount is less than the range size S, a simpler approach is to first calculate 311, 312 both the new candidate value (by incrementing a previous value by a small amount) and its replacement value in case the new candidate value is too large. The conditional test is then executed 313. If the test indicates the new candidate value is within the range, the pre-calculated new candidate value is accepted 314. If the test indicates the new candidate value is outside the range, the pre-calculated replacement candidate is accepted 315. In a particular version of this approach, the pre-calculated replacement value is calculated as C′=C−S. This essentially “wraps” the excess of the out of range value around to the beginning of the range. For example, if C=258 and S=254, then C′=4. Notably, steps 311, 312, 313, 314/315 consume the execution of multiple instructions.